package com.myhome.controller;


import com.myhome.entity.Admin;
import com.myhome.entity.RespBean;
import com.myhome.service.IAdminRoleService;
import com.myhome.service.IAdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @Autowired
    IAdminRoleService adminRoleService;

    @GetMapping("/user")
    public Object userLogin() {
        HashMap<Object, Object> result = new HashMap<>();
        result.put("name", "xiaoming");
        return result;
    }

    @ApiOperation(value = "获取搜索的操作员及其权限")
    @GetMapping("/getadmins")
    public List<Admin> getSearchToAdmin(String keywords) {
        return adminService.getSearchToAdmin(keywords);
    }

    @ApiOperation(value = "更新操作员信息")
    @PostMapping("/update")
    public RespBean updateAdmin(Admin admin) {
        if (adminService.updateById(admin)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "删除操作员信息")
    @DeleteMapping("/delete")
    public RespBean deleteAdmin(Integer id) {
        if (adminService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "更改操作员角色信息")
    @PostMapping("/getadminroles")
    public RespBean updateAdminRoles(Integer adminId,Integer[] ids){
        return adminRoleService.updateAdminRoles(adminId,ids);
    }
}
